﻿// Genius
// Copyright (C) 2016 Dirk Friedenberger, www.frittenburger.de
//   
// This source file is subject to the Microsoft Public License (Ms-PL).
// http://www.opensource.org/licenses/ms-pl.html
// All other rights reserved.
using Genius.Brain.Shared;
using Modules.VocabularyTrainer.Shared.BO;
using Modules.VocabularyTrainer.Shared.Locator;
using Modules.VocabularyTrainer.Shared.Strategy;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Modules.VocabularyTrainer.Impl.Strategy
{
    class TranslatorStrategy : ITranslatorStrategy
    {
        private IJsonDatabase<IDictionaryDatabaseRecord> _database;


        public TranslatorStrategy(string[] dictionaries)
        {
            _database = VocabularyTrainerLocator.GetDictionaryDatabase();
            foreach(var dictionary in dictionaries)
                _database.LoadFile(dictionary);
        }
       

        public IList<string> GetWords(string language, int skip,int take)
        {
            var list = new List<string>();
            foreach (var rec in _database.Data.OrderBy(rec => rec.Level).Skip(skip).Take(take))
            {
                list.Add(rec.Text[language]);
            }
            return list;
        }


        public string GetTranslation(string fromLanguage, string toLanguage, string word)
        {
            foreach (var rec in _database.Data)
            {
                var wordFrom = rec.Text[fromLanguage];
                if(wordFrom.Equals(word))
                    return rec.Text[toLanguage];
            }
            return word +  " not found in dictionary";
        }

      
    }
}
